import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { DictApi, MdItemAPI } from '#/api';

import { reactive } from 'vue';

import { DictResp } from '#/api';

const mes_item_product = reactive<DictApi.SelectOption[]>([]);
DictResp(['mes_item_product']).then((res) => {
  if (res.mes_item_product) {
    mes_item_product.push(...res.mes_item_product);
  }
});

/* 物料产品信息表格数据 */
export function useMaterialProductColumns(
  _onActionClick?: OnActionClickFn<MdItemAPI.Row>,
): VxeTableGridOptions<MdItemAPI.Row>['columns'] {
  return [
    {
      field: 'itemId',
      fixed: 'left',
      type: 'radio',
      width: 50,
    },
    {
      field: 'itemCode',
      title: '物料编码',
    },
    {
      field: 'itemName',
      title: '物料名称',
    },
    {
      field: 'specification',
      title: '规格型号',
    },
    {
      field: 'unitName',
      title: '单位',
    },
    {
      field: 'itemOrProduct',
      title: '物料/产品',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_item_product.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'itemTypeName',
      title: '所属分类',
    },
    {
      field: 'createTime',
      title: '创建时间',
    },
  ];
}

/* 物料产品信息查询表单数据 */
export function useMaterialProductScheam(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'deptId',
      dependencies: {
        triggerFields: ['deptId'],
        show: false,
      },
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入物料编码',
        allowClear: true,
      },
      fieldName: 'itemCode',
      label: '物料编码',
      labelClass: 'w-auto text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入物料名称',
        allowClear: true,
      },
      fieldName: 'itemName',
      label: '物料名称',
      labelClass: 'w-auto text-[#606266] font-bold',
    },
  ];
}
